package com.bitcola.security.applications.controller;

import com.bitcola.security.applications.biz.P2pBiz;
import com.bitcola.security.applications.entity.P2pOrder;
import com.bitcola.security.applications.mapper.P2POrderMapper;
import com.bitcola.security.common.context.BaseContextHandler;
import com.bitcola.security.common.msg.AppResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;

@RestController
@RequestMapping("p2p")
public class OtcController {

    @Autowired
    P2pBiz p2pBiz;
    @Autowired
    P2POrderMapper p2POrderMapper;
    @RequestMapping("sell")
    public AppResponse sell(BigDecimal amount)
    {
        return   AppResponse.ok().data(p2pBiz.sell(BaseContextHandler.getUserID(),amount));
    }
    @RequestMapping("buy")
    public AppResponse buy(String  secret)
    {
        return   AppResponse.ok().data(p2pBiz.buy(BaseContextHandler.getUserID(),secret));
    }

    @RequestMapping("queryBySecret")
    public AppResponse queryBySecret(String secret)
    {
        Example example=new Example(P2pOrder.class);
        example.createCriteria().andEqualTo("secret",secret);
         var list= p2POrderMapper.selectByExample(example);
         if(list.size()>0)        return AppResponse.ok().data(list.get(0));
        return AppResponse.ok().data(null);

    }

    @RequestMapping("cancel")
    public AppResponse cancel(String id)
    {
        return AppResponse.ok().data(p2pBiz.cancel(id,true));
    }

    @RequestMapping("queryOrder")
    public AppResponse queryOrder(int page,int size,String direction){

        return AppResponse.ok().data(p2pBiz.query(BaseContextHandler.getUserID(),direction,page,size));

    }


    @RequestMapping("price")
    public AppResponse pride(){

        return AppResponse.ok().data(p2pBiz.getPrice());

    }
    @RequestMapping("cancelAll")
    public AppResponse cancelAll()
    {
        return AppResponse.ok().data(p2pBiz.cancelAll());
    }











}
